#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

# The line below specified that line under it should be used as the test's short description when launching test via run_tests script.
# The text should not be longer than 80 chars - if it is, the script will strip addititonal characters
# DESCRIPTION Eviction Policy - LRU, WT

# USE_IN_NIGHTLY

# Standard beginning for every test - get the main tests directory and
# link the cas_lib file for CAS API, then use "start_test $*" to pass params
# and do other necessary checks and setup.
TESTS_DIR="$(dirname $0)/../"
. $TESTS_DIR/cas_lib
start_test $*

CACHE_DEVICE_SIZE=350M
CORE_DEVICE_SIZE=350M
TEST_DEVICE=${DEVICE_NAME}1-1
MD5_SUM=
BLOCK_SIZE="128k"

#param1 - YES - WB mode, NO - WT mode
eviction_policy_init() {
    local L_WB=$1

    # Use CACHE_DEVICE and CORE_DEVICE provided by configuration file and remove partitions from those devices
    TARGET_DEVICE_OPTION="$CACHE_DEVICE" remove_partitions
    TARGET_DEVICE_OPTION="$CORE_DEVICE" remove_partitions

    # Create 1 primary partitions on CACHE_DEVICE
    TARGET_DEVICE_OPTION="$CACHE_DEVICE" PARTITION_SIZE_OPTION=$CACHE_DEVICE_SIZE PARTITION_IDS_OPTION="1" make_primary_partitions
    # Make empty cache device, clear previous content, clear previous metadata
    dd if=/dev/zero of="${CACHE_DEVICE}-part1" bs="4k" count=$CACHE_DEVICE_SIZE &>/dev/null

    # Create 1 primary partitions on CORE_DEVICE
    TARGET_DEVICE_OPTION="$CORE_DEVICE" PARTITION_SIZE_OPTION=$CORE_DEVICE_SIZE PARTITION_IDS_OPTION="1" make_primary_partitions

    # Set WB mode
    if [ "YES" == "$L_WB" ]
    then
        CACHE_MODE_OPTION="wb"
        test_log_trace "Cache mode is WB"
    else
        CACHE_MODE_OPTION="wt"
       test_log_trace "Cache mode is WT"
    fi

    # Start cache on CACHE_DEVICE1
    CACHE_ID_OPTION="1" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part1" start_cache

    # Add a core device using CORE_DEVICE1
    CACHE_ID_OPTION="1" CORE_DEVICE_OPTION="${CORE_DEVICE}-part1" add_core
}

eviction_policy_flush() {
    CACHE_ID_OPTION="1" flush_cache
}

eviction_policy_deinit() {
    sleep 1

    # Remove the core device from cache
    CACHE_ID_OPTION="1" CORE_ID_OPTION="1" remove_core

    # Clean up after the test
    CACHE_ID_OPTION="1" stop_cache

    test_log_stop
}

# Get IO state in bytes
#param1 - Device to be stated
eviction_policy_iostat () {
    local SIZE_DEVICE
    local SIZE_WR
    local SIZE_RD
    local SIZE

    if [[ $1 =~ ^/dev/(.*)$ ]] ; then
        SIZE_DEVICE=${BASH_REMATCH[1]}
    else
        test_log_trace "Internal test ERROR ($LINENO)"
        return 1
    fi

    sync

    SIZE_WR=$(iostat -p | grep $SIZE_DEVICE | awk '{ print $6}')
    let SIZE_WR=$SIZE_WR*512

    SIZE_RD=$(iostat -p | grep $SIZE_DEVICE | awk '{ print $5}')
    let SIZE_RD=$SIZE_RD*512

    let SIZE=$SIZE_RD+$SIZE_WR

    echo $SIZE
}

# param1 - Device
# param2 - Seek in bytes
# param3 - Size in bytes
eviction_policy_write () {
    local PATTERN=/tmp/cas.pattern

    local L_DEVICE=$1

    local SEEK=$(get_bytes $2)
    SEEK=$(get_pages $SEEK "${BLOCK_SIZE}")

    local SIZE=$(get_bytes $3)
    SIZE=$(get_pages $SIZE "${BLOCK_SIZE}")

    test_log_trace "Write to the device $L_DEVICE, size is $SIZE, seek is $SEEK"

    rm -f $PATTERN

    #
    # Create pattern file
    #
    dd if=/dev/urandom of=$PATTERN bs="${BLOCK_SIZE}" count=$SIZE &>/dev/null
    #
    # Check dd result
    #
    if [ $? != 0 ]
    then
        test_log_trace "Can not create pattern file"
        return 1
    fi
    #
    # Calculte MD5 sum of pattern file
    #
    MD5_SUM=$(md5sum -b $PATTERN | awk '{ print $1 }')
    test_log_trace "MD5 sum of is $MD5_SUM"

    T_START=$(date '+%s')
    #
    # Copy pattern file into test device
    #
    warmup ${PATTERN} ${L_DEVICE} ${BLOCK_SIZE} $SIZE $SEEK 0
    #
    # Check dd result
    #
    if [ $? != 0 ]
    then
        test_log_trace "Write ERROR, at offset $OFFSET, size is $BLOCK_SIZE, device size is $DEVICE_SIZE"
        return 1
    fi

    T_STOP=$(date '+%s')
    let T_DURATION=$T_STOP-$T_START
    test_log_trace "Write duration is $T_DURATION"

    echo $MD5_SUM

    return 0
}

# param1 - Device
# param2 - Skip in bytes
# param3 - Size in bytes
eviction_policy_read () {
    local PATTERN=/tmp/cas.pattern

    local DEVICE=$1

    local SKIP=$(get_bytes $2)
    SKIP=$(get_pages $SKIP "${BLOCK_SIZE}")

    local SIZE=$(get_bytes $3)
    SIZE=$(get_pages $SIZE "${BLOCK_SIZE}")

    test_log_trace "Read from the device $DEVICE, size is $SIZE, skip is $SKIP"

    rm -f $PATTERN

    T_START=$(date '+%s')
    #
    # Read data from test device to the pattern file
    #
    warmup $DEVICE $PATTERN $BLOCK_SIZE $SIZE 0 $SKIP
    #
    # Check dd result
    #
    if [ $? != 0 ]
    then
        test_log_trace "Can not read from test device"
        return 1
    fi

    T_STOP=$(date '+%s')
    let T_DURATION=$T_STOP-$T_START
    test_log_trace "Read duration is $T_DURATION"

    #
    # Calculte MD5 sum of pattern file
    #
    MD5_SUM=$(md5sum -b $PATTERN | awk '{ print $1 }')
    test_log_trace "MD5 sum of is $MD5_SUM"

    echo $MD5_SUM

    return 0
}

#
# MAIN TEST
#
# param1 - Device for test
# param2 - Core device
# param3 - Device size [bytes]
eviction_policy_test () {
    local DEVICE=$1
    local CORE=$2
    local SIZE=$3
    local SIZE_BEFORE
    local SIZE_AFTER
    local SIZE_HALF
    local MD5_SUM_WR_1
    local MD5_SUM_WR_2

    let SIZE_HALF=$SIZE/2

    #
    # Get IO State Before
    #
    SIZE_BEFORE=$(eviction_policy_iostat $CORE)

    #
    # Write first half of device
    #
    MD5_SUM_WR_1=$(eviction_policy_write $DEVICE "0" $SIZE_HALF)
    if [ $? != 0 ]
    then
        test_log_trace "Can not write first half of test device"
        eviction_policy_deinit
        return 1
    fi

    #
    # Write second half of device
    #
    MD5_SUM_WR_2=$(eviction_policy_write $DEVICE $SIZE_HALF $SIZE_HALF)
    if [ $? != 0 ]
    then
        test_log_trace "Can not write first half of test device"
        eviction_policy_deinit
        return 1
    fi

    #
    # Get IO State Before
    #
    SIZE_BEFORE=$(eviction_policy_iostat $CORE)

    #
    # Read second half of device
    #
    MD5_SUM_RD_2=$(eviction_policy_read $DEVICE $SIZE_HALF $SIZE_HALF)
    if [ $? != 0 ]
    then
        test_log_trace "Can not write first half of test device"
        eviction_policy_deinit
        return 1
    fi

    #
    # Get IO State Before
    #
    SIZE_AFTER=$(eviction_policy_iostat $CORE)

    #
    # Read first half of device
    #
    MD5_SUM_RD_1=$(eviction_policy_read $DEVICE 0 $SIZE_HALF)
    if [ $? != 0 ]
    then
        test_log_trace "Can not write first half of test device"
        eviction_policy_deinit
        return 1
    fi

    test_log_trace "First Half WR MD5 sum is $MD5_SUM_WR_1"
    test_log_trace "First Half RD MD5 sum is $MD5_SUM_RD_1"
    test_log_trace "Second Half WR MD5 sum is $MD5_SUM_WR_2"
    test_log_trace "Second Half RD MD5 sum is $MD5_SUM_RD_2"
    test_log_trace "IO stat size of core before read first half is $SIZE_BEFORE"
    test_log_trace "IO stat size of core after read first half is $SIZE_AFTER"

    if [ $MD5_SUM_WR_1 != $MD5_SUM_RD_1 ]
    then
        eviction_policy_deinit
        error "First half checksum error"
        return 1
    fi

    if [ $MD5_SUM_WR_2 != $MD5_SUM_RD_2 ]
    then
        eviction_policy_deinit
        error "Second half checksum error"
        return 1
    fi

    if [ $SIZE_BEFORE != $SIZE_AFTER ]
    then
        eviction_policy_deinit
        error "ERROR, Size before and affter shall be the same"
        return 1
    fi

    return 0
}

#
# START TEST
#

test_log_start

run_cmd eviction_policy_init

run_cmd eviction_policy_test $TEST_DEVICE "${CORE_DEVICE}-part1" $(get_bytes $CORE_DEVICE_SIZE)

run_cmd eviction_policy_deinit

#
# END TEST
#

# Always return 0 at the end of the test - if at any point something has failed
# in the API functions, test will end and return a proper result.
# If you need to check other things during the test and end the test earlier, you
# should end the test using "end_test $retval" function
end_test 0
